#include "special_trigger.h"
#include "signal_catch.h"

#include "data_cache.h"
#include "public_define.h"
#include <typeinfo>
#include "busesp.pb.h"
#include "common_opr_all.h"
#include "tool.h"

using namespace DcAdr;
using namespace std;


map<string, funcp> m_triggerFunc = {
    {"BA009", DcAdr::SpecialTrigger::inner_event_judge_BA009},//过程事件
    {"BA530", DcAdr::SpecialTrigger::inner_event_judge_BA530},
    {"BA531", DcAdr::SpecialTrigger::inner_event_judge_BA531},
    {"BA532", DcAdr::SpecialTrigger::inner_event_judge_BA532},
    {"BA007", DcAdr::SpecialTrigger::inner_event_judge_BA007},//过程事件
    {"IT574", DcAdr::SpecialTrigger::inner_event_judge_IT574},
    {"IT577", DcAdr::SpecialTrigger::inner_event_judge_IT577},
    {"BA501", DcAdr::SpecialTrigger::inner_event_judge_BA501},
    //{"BA799", DcAdr::SpecialTrigger::inner_event_judge_BA799},
};

//event_BA009_para_stru SpecialTrigger::event_BA009_para = {0};
//event_BA530_para_stru SpecialTrigger::event_BA530_para = {0};
//event_BA531_para_stru SpecialTrigger::event_BA531_para = {0};
//event_BA532_para_stru SpecialTrigger::event_BA532_para = {0};
//event_BA007_para_stru SpecialTrigger::event_BA007_para = {0};
//event_IT574_para_stru SpecialTrigger::event_IT574_para = {0};
//event_IT577_para_stru SpecialTrigger::event_IT577_para = {0};
//event_BA501_para_stru SpecialTrigger::event_BA501_para = {0};
//event_BA799_para_stru SpecialTrigger::event_BA799_para = {0};
event_BA009_para_stru SpecialTrigger::event_BA009_para = {};
event_BA530_para_stru SpecialTrigger::event_BA530_para = {};
event_BA531_para_stru SpecialTrigger::event_BA531_para = {};
event_BA532_para_stru SpecialTrigger::event_BA532_para = {};
event_BA007_para_stru SpecialTrigger::event_BA007_para = {};
event_IT574_para_stru SpecialTrigger::event_IT574_para = {};
event_IT577_para_stru SpecialTrigger::event_IT577_para = {};
event_BA501_para_stru SpecialTrigger::event_BA501_para = {};
event_BA799_para_stru SpecialTrigger::event_BA799_para = {};

double SpecialTrigger::K_ADR_LatAccelMaxValue = 5.5;
double SpecialTrigger::K_ADR_LongAccelMinValue = -6.5;


SpecialTrigger::SpecialTrigger()
{
    //BA532初始值赋值(0xFF为初始化无效值)
    event_BA532_para.old_ib_brk_pedl_sts_gb = 0xFF;

    //IT574初始值赋值(10001为初始化无效值)
    event_IT574_para.old_takeover_alarm_lat_level = 10001;
    event_IT574_para.old_takeover_alarm_lng_level = 10001;
}

SpecialTrigger::~SpecialTrigger()
{
}


SpecialTrigger &SpecialTrigger::getInstance()
{
    static SpecialTrigger adr;
    return adr;
}

void SpecialTrigger::push2TopicMap(const std::string &topic, const std::string &eventid)
{
    m_spTrg_topicEventIDList[topic].push_back(eventid);
}

bool SpecialTrigger::isContainTopic(const std::string &topic)
{
    return Contain_Map(m_spTrg_topicEventIDList, topic);
}

void SpecialTrigger::addTopic2Vec(const std::vector<std::string> &srcVec, std::vector<std::string> &desVec)
{
    for (auto it = srcVec.begin(); it != srcVec.end(); it++)
    {
        desVec.push_back(*it);
    }
}




void SpecialTrigger::inner_event_judge(const ProtoData &data, const std::shared_ptr<ProtoCondInfo_t> protoInfo)
{
    double currTime = Util::getMsTime();

    std::string topic = *data.topic.get(); 
    std::string eventId = protoInfo->eventName;

    topic_data_parse_and_save(data);

    //非过程事件限制触发频率
    if (eventId != "BA009" && eventId != "BA007")
    {
        if ((currTime - protoInfo->trigLaterTime) < protoInfo->limitTime)
        {
            return;
        }
    }

    if (m_triggerFunc.find(eventId) != m_triggerFunc.end())
    {
        TriggerEventInfo trigger;
        auto fun = m_triggerFunc[eventId];
        if (fun(eventId, data, trigger))
        {
            trigger.beforeTime = protoInfo->beforeTime;
            trigger.laterTime = protoInfo->laterTime;
            trigger.collectDatas = protoInfo->collectData;
            trigger.level = protoInfo->level;
            trigger.triggerStartTime =KTime::getPubTime();
            //信号触发,存文件。
            Log_Info("Proto Event happen event name: {}  start", trigger.eventName);
            DataCache::getInstance().acquireDataToSave(trigger);
        }
    }
}

/* 有碰撞风险事件判断(过程事件) */
unsigned char SpecialTrigger::inner_event_judge_BA007(const string eventId, const ProtoData &data, TriggerEventInfo &retTigger)
{
    unsigned char tmp_event_status = 0;
    int tmp_topic_id = inner_event_get_topic_id(*data.topic.get());
    (void)tmp_topic_id;

    get_signal_from_map("sigkey_PlanningOutput_accel_ref", &(event_BA007_para.var2));
    get_signal_from_map("sigkey_FromSTS_sys_latstate", &(event_BA007_para.sys_latstate));
    get_signal_from_map("sigkey_FromSTS_pd_status", &(event_BA007_para.m_pd_status));

    if ((event_BA007_para.m_pd_status == 0x3  || event_BA007_para.m_pd_status == 0x4) && 
        (event_BA007_para.sys_latstate == 0x1   || event_BA007_para.sys_latstate == 0x2) && 
        event_BA007_para.var2 <= (-5) && 
        event_BA007_para.event_happen_flag == 0)
    {
        tmp_event_status = 1;
        event_BA007_para.event_happen_flag = 1;

        retTigger.eventName = eventId;
        retTigger.status = TS_START;
        retTigger.triggerTime = Util::getMsTime();
        retTigger.recordTime = CommonOprTime::getTime();
        retTigger.beforeTime = 15000;
        retTigger.laterTime = 5000;
    }

    if (((event_BA007_para.m_pd_status != 0x3 && event_BA007_para.m_pd_status != 0x4) || 
            (event_BA007_para.sys_latstate != 0x1  && event_BA007_para.sys_latstate != 0x2) || 
                event_BA007_para.var2 > -3) && event_BA007_para.event_happen_flag == 1)
    {
        tmp_event_status = 1;
        event_BA007_para.event_happen_flag = 0;

        retTigger.eventName = eventId;
        retTigger.status = TS_END;
        retTigger.triggerTime = Util::getMsTime();
        retTigger.recordTime = CommonOprTime::getTime();
        retTigger.beforeTime = 15000;
        retTigger.laterTime = 5000;
    }

    return tmp_event_status;
}


/* 触发立即接管异常事件判断(瞬时事件) */
unsigned char SpecialTrigger::inner_event_judge_BA532(const string eventId, const ProtoData &data, TriggerEventInfo &retTigger)
{
    unsigned char tmp_event_status = 0;
    int tmp_topic_id = inner_event_get_topic_id(*data.topic.get());

    get_signal_from_map("sigkey_BasicStateFusion_ad_lattakeoverreq", &(event_BA532_para.ad_lattakeoverreq));
    get_signal_from_map("sigkey_BusEps_eps_steer_ag_rate", &(event_BA532_para.eps_steer_ag_rate));
    get_signal_from_map("sigkey_BusEps_eps_measured_torsion_bar_torque", &(event_BA532_para.eps_measured_torsion_bar_torque));
    get_signal_from_map("sigkey_BusEps_eps_measured_torsion_bar_torq_valid", &(event_BA532_para.eps_measured_torsion_bar_torq_valid));

    get_signal_from_map("sigkey_BasicStateFusion_ad_lngtakeoverreq", &(event_BA532_para.ad_lngtakeoverreq));
    get_signal_from_map("sigkey_Vcu_ib_brk_pedl_sts_gb", &(event_BA532_para.ib_brk_pedl_sts_gb));

    if ((event_BA532_para.ad_lattakeoverreq == 0x1 && event_BA532_para.old_ad_lattakeoverreq != 0x1) || 
        (event_BA532_para.ad_lattakeoverreq == 0x2 && event_BA532_para.old_ad_lattakeoverreq != 0x2))
    {
        event_BA532_para.alarm_lat_time = data.time;
    }

    if ((event_BA532_para.ad_lngtakeoverreq == 0x1 && event_BA532_para.old_ad_lngtakeoverreq != 0x1) || 
        (event_BA532_para.ad_lngtakeoverreq == 0x2 && event_BA532_para.old_ad_lngtakeoverreq != 0x2))
    {
        event_BA532_para.alarm_lng_time = data.time;
    }

    if (TOPIC_ID_hmi_interface_HmiInterface_BasicStateFusion == tmp_topic_id)
    {
        event_BA532_para.old_ad_lattakeoverreq = event_BA532_para.ad_lattakeoverreq;
        event_BA532_para.old_ad_lngtakeoverreq = event_BA532_para.ad_lngtakeoverreq;
    }

    //1
    if ((event_BA532_para.ad_lattakeoverreq == 0x1 || event_BA532_para.ad_lattakeoverreq == 0x2) && 
        ((event_BA532_para.eps_steer_ag_rate != 0xff && event_BA532_para.eps_steer_ag_rate > K_ADR_TakerOverSASspd) 
        || (event_BA532_para.eps_measured_torsion_bar_torq_valid == 0 && fabs(event_BA532_para.eps_measured_torsion_bar_torque) > K_ADR_TakerOverTorq)) && 
        (data.time - event_BA532_para.alarm_lat_time) < 2 *1000)
    {
        if (event_BA532_para.event_happen_flag != 1)
        {
            tmp_event_status = 1;
            event_BA532_para.event_happen_flag = 1;

            retTigger.eventName = eventId;
            retTigger.triggerTime = Util::getMsTime();
            retTigger.recordTime = CommonOprTime::getTime();
            retTigger.beforeTime = 15000;
            retTigger.laterTime = 5000;
        }
        return tmp_event_status;
    }
    else
    {
        event_BA532_para.event_happen_flag = 0;
    }

    //2
    if ((event_BA532_para.ad_lngtakeoverreq == 0x1 || event_BA532_para.ad_lngtakeoverreq == 0x2) && event_BA532_para.ib_brk_pedl_sts_gb == 0x65 && 
        event_BA532_para.old_ib_brk_pedl_sts_gb == 0x00 && ((data.time - event_BA532_para.alarm_lng_time) < 2 * 1000)  )
    {
        if (event_BA532_para.event_happen_flag != 1)
        {
            tmp_event_status = 1;
            event_BA532_para.event_happen_flag = 1;

            retTigger.eventName = eventId;
            retTigger.triggerTime = Util::getMsTime();
            retTigger.recordTime = CommonOprTime::getTime();
            retTigger.beforeTime = 15000;
            retTigger.laterTime = 5000;
        }
    }
    else
    {
        event_BA532_para.event_happen_flag = 0;
    }

    if (TOPIC_ID_sab_VcuProto_Vcu == tmp_topic_id)
    {
        event_BA532_para.old_ib_brk_pedl_sts_gb = event_BA532_para.ib_brk_pedl_sts_gb;
    }

    return tmp_event_status;
}

/* 其他潜在危险事件判断（瞬时）BA531 */
unsigned char SpecialTrigger::inner_event_judge_BA531(const string eventId, const ProtoData &data, TriggerEventInfo &retTigger)
{
    unsigned char tmp_event_status = 0;

    int tmp_topic_id = inner_event_get_topic_id(*data.topic.get());
    (void)tmp_topic_id;

    get_signal_from_map("sigkey_FromSTS_sys_latstate", &(event_BA531_para.sys_latstate));
    get_signal_from_map("sigkey_AebResult_fcw_active", &(event_BA531_para.fcw_active));
    get_signal_from_map("sigkey_AebResult_awb_active", &(event_BA531_para.awb_active));
    get_signal_from_map("sigkey_BusEsp_esp_long_accel", &(event_BA531_para.esp_long_accel));
    get_signal_from_map("sigkey_BusEsp_esp_long_accel_valid", &(event_BA531_para.esp_long_accel_valid));
    get_signal_from_map("sigkey_FromSTS_pd_status", &(event_BA531_para.pd_status));
    get_signal_from_map("sigkey_FromSTS_sys_lngstate", &(event_BA531_para.sys_lngstate));
    get_signal_from_map("sigkey_LasSystemState_elk_state", &(event_BA531_para.elk_state));


    if ((event_BA531_para.sys_latstate == 0x1 || event_BA531_para.sys_latstate == 0x2) && (event_BA531_para.awb_active == true || event_BA531_para.fcw_active == true))
    {
        if (CompareValueIsZero(event_BA531_para.timestamp) == true)
        {
            event_BA531_para.timestamp = data.time;
        }

        if ((event_BA531_para.esp_long_accel_valid == 0x01) && (event_BA531_para.esp_long_accel <= K_ADR_OtherDangerJudgeMaxAcc) && ((data.time - event_BA531_para.timestamp) < 2 * 1000))
        {
            if (event_BA531_para.event_happen_flag != 1)
            {
                tmp_event_status = 1;                
                event_BA531_para.event_happen_flag = 1;

                retTigger.eventName = eventId;
                retTigger.triggerTime = Util::getMsTime();
                retTigger.recordTime = CommonOprTime::getTime();
                retTigger.beforeTime = 15000;
                retTigger.laterTime = 5000;
            }
        }

        return tmp_event_status;
    }
    else
    {
        event_BA531_para.timestamp = 0;
        event_BA531_para.event_happen_flag = 0;
    }

    if (event_BA531_para.pd_status == 0x1 && (event_BA531_para.sys_lngstate == 0x1 || event_BA531_para.sys_lngstate == 0x2) && event_BA531_para.elk_state == 0x3 )
    {
        if (event_BA531_para.event_happen_flag != 1)
        {
            tmp_event_status = 1;                
            event_BA531_para.event_happen_flag = 1;

            retTigger.eventName = eventId;
            retTigger.triggerTime = Util::getMsTime();
            retTigger.recordTime = CommonOprTime::getTime();
            retTigger.beforeTime = 15000;
            retTigger.laterTime = 5000;
        }
    }
    else
    {
        event_BA531_para.event_happen_flag = 0;
    }

    return tmp_event_status;
}

/* 系统逻辑异常事件判断(瞬时事件) */
unsigned char SpecialTrigger::inner_event_judge_BA530(const string eventId, const ProtoData &data, TriggerEventInfo &retTigger)
{
    unsigned char tmp_event_status = 0;

    int tmp_topic_id = inner_event_get_topic_id(*data.topic.get());
    (void)tmp_topic_id;
    get_signal_from_map("sigkey_Vcu_ib_brk_pedl_sts_gb", &(event_BA530_para.ib_brk_pedl_sts_gb));
    get_signal_from_map("sigkey_Vcu_vcu_ovrd_mod_req", &(event_BA530_para.vcu_ovrd_mod_req));

    get_signal_from_map("sigkey_LongCtrlCmdOutProto_acc_acctrqreq", &(event_BA530_para.acc_acctrqreq));
    get_signal_from_map("sigkey_LongCtrlCmdOutProto_acc_acctrqreqena", &(event_BA530_para.acc_acctrqreqena));
    get_signal_from_map("sigkey_LongCtrlCmdOutProto_acc_targetacceleration", &(event_BA530_para.acc_targetacceleration));
    get_signal_from_map("sigkey_LongCtrlCmdOutProto_acc_cddaxena", &(event_BA530_para.acc_cddaxena));

    if (event_BA530_para.ib_brk_pedl_sts_gb == 0x65 && event_BA530_para.acc_acctrqreqena == 1 && event_BA530_para.acc_acctrqreq > K_ADR_LogicFaultTrq)
    {
        if (CompareValueIsZero(event_BA530_para.first_cond_trigger_time) == true)
        {
            event_BA530_para.first_cond_trigger_time = data.time;
        }
        else if (CompareValueIsZero(event_BA530_para.first_cond_trigger_time) == false
                 && (data.time - event_BA530_para.first_cond_trigger_time) > (1000))
        {

            if (event_BA530_para.event_happen_flag != 1)
            {
                tmp_event_status = 1;                
                event_BA530_para.event_happen_flag = 1;

                retTigger.eventName = eventId;
                retTigger.triggerTime = Util::getMsTime();
                retTigger.recordTime = CommonOprTime::getTime();
                retTigger.beforeTime = 15000;
                retTigger.laterTime = 5000;
            }

            return tmp_event_status;
        }
    }
    else
    {
        event_BA530_para.first_cond_trigger_time = 0;
        event_BA530_para.event_happen_flag = 0;
    }

    if (((event_BA530_para.acc_targetacceleration)< K_ADR_LogicFaultAcceleration) && event_BA530_para.acc_cddaxena == true && event_BA530_para.vcu_ovrd_mod_req == 1)
    {

        if (CompareValueIsZero(event_BA530_para.second_cond_trigger_time) == true)
        {
            event_BA530_para.second_cond_trigger_time = data.time;
        }
        else if (CompareValueIsZero(event_BA530_para.second_cond_trigger_time) == false
                 && (data.time - event_BA530_para.second_cond_trigger_time) > (1000))
        {

            if (event_BA530_para.event_happen_flag != 1)
            {
                tmp_event_status = 1;                
                event_BA530_para.event_happen_flag = 1;

                retTigger.eventName = eventId;
                retTigger.triggerTime = Util::getMsTime();
                retTigger.recordTime = CommonOprTime::getTime();
                retTigger.beforeTime = 15000;
                retTigger.laterTime = 5000;
            }
        }
    }
    else
    {
        event_BA530_para.second_cond_trigger_time = 0;
        event_BA530_para.event_happen_flag = 0;
    }

    return tmp_event_status;
}

unsigned char SpecialTrigger::inner_event_judge_BA009(const string eventId, const ProtoData &data, TriggerEventInfo &retTigger)
{
    unsigned char tmp_event_status = 0;
    int tmp_topic_id = inner_event_get_topic_id(*data.topic.get());

    get_signal_from_map("sigkey_BusEsp_esp_long_accel", &(event_BA009_para.esp_long_accel));
    get_signal_from_map("sigkey_BusEsp_esp_lat_accel", &(event_BA009_para.esp_lat_accel));
    get_signal_from_map("sigkey_BusEsp_esp_veh_spd", &(event_BA009_para.esp_veh_spd));
    get_signal_from_map("sigkey_BusEsp_esp_long_accel_valid", &(event_BA009_para.esp_long_accel_valid));
    get_signal_from_map("sigkey_BusEsp_esp_lat_accel_valid", &(event_BA009_para.esp_lat_accel_valid));

    if (TOPIC_ID_sab_BusespProto_BusEsp == tmp_topic_id)    
    {
        if (event_BA009_para.esp_veh_spd > 0 && event_BA009_para.esp_veh_spd <= 40)
        {
            K_ADR_LatAccelMaxValue = 5.5;
            K_ADR_LongAccelMinValue = -6.5;
        }
        else if (event_BA009_para.esp_veh_spd > 40 && event_BA009_para.esp_veh_spd <= 80)
        {
            K_ADR_LatAccelMaxValue = 4;
            K_ADR_LongAccelMinValue = -5.5;
        }
        else if (event_BA009_para.esp_veh_spd > 80 )
        {
            K_ADR_LatAccelMaxValue = 3.5;
            K_ADR_LongAccelMinValue = -4.5;
        }

        double tmp_esp_lat_accel = (double)event_BA009_para.esp_lat_accel;
        if (tmp_esp_lat_accel < 0)
        {
            tmp_esp_lat_accel = 0 - tmp_esp_lat_accel;
        }

        //开始条件
        if ((((CompareValue(tmp_esp_lat_accel, K_ADR_LatAccelMaxValue) == CMP_GREATOR) && (!CompareValueIsZero(event_BA009_para.esp_lat_accel - 0xFFFF)) &&  CompareValueIsZero(event_BA009_para.esp_lat_accel_valid - 0x01) ) ||
             ((CompareValue(event_BA009_para.esp_long_accel, K_ADR_LongAccelMinValue) == CMP_LOWER) && (!(CompareValueIsZero(event_BA009_para.esp_long_accel - 0xFFFF)))  &&  CompareValueIsZero(event_BA009_para.esp_long_accel_valid - 0x01) )) &&
            event_BA009_para.event_happen_flag == 0)
        {
            event_BA009_para.event_happen_flag = 1;

            tmp_event_status = 1;
            retTigger.eventName = eventId;
            retTigger.status = TS_START;
            retTigger.triggerTime = Util::getMsTime();
            retTigger.recordTime = CommonOprTime::getTime();
            retTigger.beforeTime = 15000;
            retTigger.laterTime = 5000;
        }

        //结束条件
        if (((((tmp_esp_lat_accel - K_ADR_LatAccelMinValue) < 0.000001) && ((event_BA009_para.esp_long_accel - K_ADR_LongAccelMaxValue) > 0.000001)) || 
            (CompareValueIsZero(event_BA009_para.esp_lat_accel - 0xFFFF)) || (CompareValueIsZero(event_BA009_para.esp_long_accel - 0xFFFF))) && (event_BA009_para.event_happen_flag == 1))
        {
            event_BA009_para.event_happen_flag = 0;

            tmp_event_status = 1;
            retTigger.eventName = eventId;
            retTigger.status = TS_END;
            retTigger.triggerTime = Util::getMsTime();
            retTigger.recordTime = CommonOprTime::getTime();
            retTigger.beforeTime = 15000;
            retTigger.laterTime = 5000;
        }
    }

    return tmp_event_status;
}

// IT574 接管报警退出
unsigned char SpecialTrigger::inner_event_judge_IT574(const string eventId, const ProtoData &data, TriggerEventInfo &retTigger)
{
    unsigned char tmp_event_status = 0;
    int tmp_topic_id = inner_event_get_topic_id(*data.topic.get());
    (void)tmp_topic_id;

    get_signal_from_map("sigkey_FromSTS_takeover_alarm_lat_level", &(event_IT574_para.takeover_alarm_lat_level));
    get_signal_from_map("sigkey_FromSTS_takeover_alarm_lng_level", &(event_IT574_para.takeover_alarm_lng_level));

    //1、<SST_纵向接管请求>SST_TakeoverWarningLngLevel：由一级报接管报警 或 二级接管报警 变为 无请求或报警完成 ；且<SST_横向接管请求>SST_TakeoverWarningLatLevel：由一级报接管报警 或 二级接管报警 变为 无请求或报警完成 ；
    if (((event_IT574_para.old_takeover_alarm_lng_level == 1 && event_IT574_para.takeover_alarm_lng_level == 0) || 
         (event_IT574_para.old_takeover_alarm_lng_level == 1 && event_IT574_para.takeover_alarm_lng_level == 3) || 
         (event_IT574_para.old_takeover_alarm_lng_level == 2 && event_IT574_para.takeover_alarm_lng_level == 0) || 
         (event_IT574_para.old_takeover_alarm_lng_level == 2 && event_IT574_para.takeover_alarm_lng_level == 3)) &&
        ((event_IT574_para.old_takeover_alarm_lat_level == 1 && event_IT574_para.takeover_alarm_lat_level == 0) || 
         (event_IT574_para.old_takeover_alarm_lat_level == 1 && event_IT574_para.takeover_alarm_lat_level == 3) || 
         (event_IT574_para.old_takeover_alarm_lat_level == 2 && event_IT574_para.takeover_alarm_lat_level == 0) || 
         (event_IT574_para.old_takeover_alarm_lat_level == 2 && event_IT574_para.takeover_alarm_lat_level == 3)))
    {
        tmp_event_status = 1;
        retTigger.eventName = eventId;
        retTigger.triggerTime = Util::getMsTime();
        retTigger.recordTime = CommonOprTime::getTime();
        retTigger.beforeTime = 0;
        retTigger.laterTime = 1000;

        event_IT574_para.old_takeover_alarm_lng_level = event_IT574_para.takeover_alarm_lng_level;
        event_IT574_para.old_takeover_alarm_lat_level = event_IT574_para.takeover_alarm_lat_level;

        return tmp_event_status;
    }

    //2、<SST_纵向接管请求>SST_TakeoverWarningLngLevel为： 无请求或报警完成 ；且<SST_横向接管请求>SST_TakeoverWarningLatLevel：由一级报接管报警 或 二级接管报警 变为 无请求或报警完成 ；
    if (event_IT574_para.takeover_alarm_lng_level == 0 || event_IT574_para.takeover_alarm_lng_level == 3)
    {
        if ((event_IT574_para.old_takeover_alarm_lat_level == 1 && event_IT574_para.takeover_alarm_lat_level == 0) || 
            (event_IT574_para.old_takeover_alarm_lat_level == 1 && event_IT574_para.takeover_alarm_lat_level == 3) || 
            (event_IT574_para.old_takeover_alarm_lat_level == 2 && event_IT574_para.takeover_alarm_lat_level == 0) || 
            (event_IT574_para.old_takeover_alarm_lat_level == 2 && event_IT574_para.takeover_alarm_lat_level == 3))
        {

            tmp_event_status = 1;
            retTigger.eventName = eventId;
            retTigger.triggerTime = Util::getMsTime();
            retTigger.recordTime = CommonOprTime::getTime();
            retTigger.beforeTime = 0;
            retTigger.laterTime = 1000;

            event_IT574_para.old_takeover_alarm_lng_level = event_IT574_para.takeover_alarm_lng_level;
            event_IT574_para.old_takeover_alarm_lat_level = event_IT574_para.takeover_alarm_lat_level;

            return tmp_event_status;
        }
    }


    //3、<SST_横向接管请求>SST_TakeoverWarningLatLevel为： 无请求或报警完成 ； 且<SST_纵向接管请求>SST_TakeoverWarningLngLevel：由一级报接管报警 或 二级接管报警 变为 无请求或报警完成 ；"
    if (event_IT574_para.takeover_alarm_lat_level == 0 || event_IT574_para.takeover_alarm_lat_level == 3)
    {

        if ((event_IT574_para.old_takeover_alarm_lng_level == 1 && event_IT574_para.takeover_alarm_lng_level == 0) || 
            (event_IT574_para.old_takeover_alarm_lng_level == 1 && event_IT574_para.takeover_alarm_lng_level == 3) || 
            (event_IT574_para.old_takeover_alarm_lng_level == 2 && event_IT574_para.takeover_alarm_lng_level == 0) || 
            (event_IT574_para.old_takeover_alarm_lng_level == 2 && event_IT574_para.takeover_alarm_lng_level == 3))
        {
            tmp_event_status = 1;
            retTigger.eventName = eventId;
            retTigger.triggerTime = Util::getMsTime();
            retTigger.recordTime = CommonOprTime::getTime();
            retTigger.beforeTime = 0;
            retTigger.laterTime = 1000;

            event_IT574_para.old_takeover_alarm_lng_level = event_IT574_para.takeover_alarm_lng_level;
            event_IT574_para.old_takeover_alarm_lat_level = event_IT574_para.takeover_alarm_lat_level;

            return tmp_event_status;
        }
    }

    event_IT574_para.old_takeover_alarm_lng_level = event_IT574_para.takeover_alarm_lng_level;
    event_IT574_para.old_takeover_alarm_lat_level = event_IT574_para.takeover_alarm_lat_level;

    return tmp_event_status;    
}
#if 1
// IT577 车辆严重故障
unsigned char SpecialTrigger::inner_event_judge_IT577(const string eventId, const ProtoData &data, TriggerEventInfo &retTigger)
{
    unsigned char tmp_event_status = 0;
    int tmp_topic_id = inner_event_get_topic_id(*data.topic.get());
    (void)tmp_topic_id;

    get_signal_from_map("sigkey_FromSTS_pd_status", &(event_IT577_para.m_pd_status));
    get_signal_from_map("sigkey_BusEps_eps_faild", &(event_IT577_para.m_eps_faild));
    get_signal_from_map("sigkey_BusEsp_esp_tcs_failr", &(event_IT577_para.m_esp_tcs_failr));
    get_signal_from_map("sigkey_Vcu_vcu_pt_tq_req_avl", &(event_IT577_para.m_vcu_pt_tq_req_avl));
    //Log_ERROR("event_IT577_para.m_eps_faild:{},event_IT577_para.m_esp_tcs_failr:{},event_IT577_para.m_vcu_pt_tq_req_avl:{}",event_IT577_para.m_eps_faild,event_IT577_para.m_esp_tcs_failr,event_IT577_para.m_vcu_pt_tq_req_avl);
    if (event_IT577_para.m_pd_status == 0x01 || event_IT577_para.m_pd_status == 0x02 || event_IT577_para.m_pd_status == 0x03 || event_IT577_para.m_pd_status == 0x04)
    {
        if (event_IT577_para.m_eps_faild == 0x03)
        {
            if (CompareValueIsZero(event_IT577_para.first_cond_trigger_time) == true)
            {
                event_IT577_para.first_cond_trigger_time = data.time;
            }
            else if (CompareValueIsZero(event_IT577_para.first_cond_trigger_time) == false
                     && (data.time - event_IT577_para.first_cond_trigger_time) > (100))
            {

                if (event_IT577_para.event_happen_flag != 1)
                {
                    tmp_event_status = 1;
                    event_IT577_para.event_happen_flag = 1;

                    retTigger.eventName = eventId;
                    retTigger.triggerTime = Util::getMsTime();
                    retTigger.recordTime = CommonOprTime::getTime();
                    retTigger.beforeTime = 15000;
                    retTigger.laterTime = 5000;
                }
            }
        }
        else
        {
            event_IT577_para.first_cond_trigger_time=0.0;
            event_IT577_para.event_happen_flag=0;
        }
        if (event_IT577_para.m_esp_tcs_failr == 0x01)
        {
            if (CompareValueIsZero(event_IT577_para.second_cond_trigger_time) == true)
            {
                event_IT577_para.second_cond_trigger_time = data.time;
            }
            else if (CompareValueIsZero(event_IT577_para.second_cond_trigger_time) == false
                     && (data.time - event_IT577_para.second_cond_trigger_time) > (100))
            {

                if (event_IT577_para.event_happen_flag != 1)
                {
                    tmp_event_status = 1;
                    event_IT577_para.event_happen_flag = 1;

                    retTigger.eventName = eventId;
                    retTigger.triggerTime = Util::getMsTime();
                    retTigger.recordTime = CommonOprTime::getTime();
                    retTigger.beforeTime = 15000;
                    retTigger.laterTime = 5000;
                }
            }
        }
        else
        {
            event_IT577_para.second_cond_trigger_time=0.0;
            event_IT577_para.event_happen_flag=0;
        }
        if (event_IT577_para.m_vcu_pt_tq_req_avl == 0x0)
        {
            if (CompareValueIsZero(event_IT577_para.third_cond_trigger_time) == true)
            {
                event_IT577_para.third_cond_trigger_time = data.time;
            }
            else if (CompareValueIsZero(event_IT577_para.third_cond_trigger_time) == false
                     && (data.time - event_IT577_para.third_cond_trigger_time) > (100))
            {

                if (event_IT577_para.event_happen_flag != 1)
                {
                    tmp_event_status = 1;
                    event_IT577_para.event_happen_flag = 1;

                    retTigger.eventName = eventId;
                    retTigger.triggerTime = Util::getMsTime();
                    retTigger.recordTime = CommonOprTime::getTime();
                    retTigger.beforeTime = 15000;
                    retTigger.laterTime = 5000;
                }
            }
        }
        else
        {
            event_IT577_para.third_cond_trigger_time=0.0;
            event_IT577_para.event_happen_flag=0;
        }
    }

    return tmp_event_status;
}
#endif


/* 泊车过程主动退出事件 */
unsigned char SpecialTrigger::inner_event_judge_BA501(const string eventId, const ProtoData &data, TriggerEventInfo &retTigger)
{
    unsigned char tmp_event_status = 0;

    int tmp_topic_id = inner_event_get_topic_id(*data.topic.get());
    (void)tmp_topic_id;

    get_signal_from_map("sigkey_FromSTS_apa_status", &(event_BA501_para.apa_status));
    get_signal_from_map("sigkey_FromSTS_park_sst_req", &(event_BA501_para.park_sst_req));
    get_signal_from_map("sigkey_FromSTS_park_sst_notice", &(event_BA501_para.park_sst_notice));
    get_signal_from_map("sigkey_HmiParkInternal_hmi_apadrvrreqonoff", &(event_BA501_para.hmi_apadrvrreqonoff));

    //if ((event_BA501_para.park_sst_req == 0x6 &&  (event_BA501_para.park_sst_notice == 0x1 || event_BA501_para.park_sst_notice == 0x3)) ||
    //    (event_BA501_para.hmi_apadrvrreqonoff == 0x2 &&  (event_BA501_para.apa_status == 0x3 || event_BA501_para.apa_status == 0x4 || event_BA501_para.apa_status == 0x7)))
    //C798 变更触发条件
    if((event_BA501_para.park_sst_req ==0x3 || event_BA501_para.park_sst_req ==0x4 ||event_BA501_para.park_sst_req ==0x6 ||event_BA501_para.park_sst_req ==0x7)
    &&(event_BA501_para.park_sst_notice==0x1 ||event_BA501_para.park_sst_notice==0x3 ||event_BA501_para.park_sst_notice==0x4 ||event_BA501_para.park_sst_notice==0x5 ||event_BA501_para.park_sst_notice==0x6 ||event_BA501_para.park_sst_notice==0x7
    ||event_BA501_para.park_sst_notice==39 ||event_BA501_para.park_sst_notice==44 ||event_BA501_para.park_sst_notice==58 ||event_BA501_para.park_sst_notice==67 || event_BA501_para.park_sst_notice==90 ||event_BA501_para.park_sst_notice==91 ||event_BA501_para.park_sst_notice==92
    ||event_BA501_para.park_sst_notice==93 ||event_BA501_para.park_sst_notice==94 ||event_BA501_para.park_sst_notice==95 ||event_BA501_para.park_sst_notice==98 ||event_BA501_para.park_sst_notice==106 ||event_BA501_para.park_sst_notice==109 ||event_BA501_para.park_sst_notice==131
    ||event_BA501_para.park_sst_notice==132 ||event_BA501_para.park_sst_notice==133 ||event_BA501_para.park_sst_notice==134 ||event_BA501_para.park_sst_notice==140))
    {
        tmp_event_status = 1;

        retTigger.eventName = eventId;
        retTigger.triggerTime = Util::getMsTime();
        retTigger.recordTime = CommonOprTime::getTime();
        retTigger.beforeTime = 9000;
        retTigger.laterTime = 1000;
    }

    return tmp_event_status;
}
/* 高危险场景事件 BA799*/
unsigned char SpecialTrigger::inner_event_judge_BA799(const string eventId, const ProtoData &data, TriggerEventInfo &retTigger)
{
    unsigned char tmp_event_status = 0;

    int tmp_topic_id = inner_event_get_topic_id(*data.topic.get());
    (void)tmp_topic_id;
    get_signal_from_map("sigkey_FusionProto_lateral_distance",&(event_BA799_para.lateral_distance));
    get_signal_from_map("sigkey_FromSTS_sys_lngstate", &(event_BA799_para.sys_lngstate));
    get_signal_from_map("sigkey_BusEsp_esp_veh_spd", &(event_BA799_para.esp_veh_spd));
    get_signal_from_map("sigkey_FusionProto_longitudinal_absolute_velocity", &(event_BA799_para.longitudinal_absolute_velocity));
    get_signal_from_map("sigkey_FusionProto_longitudinal_distance", &(event_BA799_para.longitudinal_distance));
    get_signal_from_map("sigkey_FusionProto_width", &(event_BA799_para.width));
    get_signal_from_map("sigkey_FromSTS_pd_status", &(event_BA799_para.pd_status));
    get_signal_from_map("sigkey_FromSTS_sys_latstate", &(event_BA799_para.sys_latstate));
    get_signal_from_map("sigkey_busadasmap_hu_nav_guigance_status", &(event_BA799_para.hu_nav_guigance_status));
    get_signal_from_map("sigkey_busadasmap_hu_road_curvature", &(event_BA799_para.hu_road_curvature));
    get_signal_from_map("sigkey_BusEsp_esp_long_accel", &(event_BA799_para.esp_long_accel));
    //case 1 2 3
    //Log_Info("case1 sys_lngstate:{},esp_veh_spd:{},lateral_distance:{},longitudinal_absolute_velocity:{},longitudinal_distance:{},first_cond_trigger_time:{},data_time:{}",event_BA799_para.sys_lngstate,event_BA799_para.esp_veh_spd
    //,event_BA799_para.lateral_distance,event_BA799_para.longitudinal_absolute_velocity,event_BA799_para.longitudinal_distance,event_BA799_para.first_cond_trigger_time,data.time);
    //Log_Info("jisuan,1:{},2:{},3:{}",fabs(event_BA799_para.lateral_distance),event_BA799_para.esp_veh_spd-event_BA799_para.longitudinal_absolute_velocity*3.6,event_BA799_para.longitudinal_distance/(event_BA799_para.esp_veh_spd/3.6-event_BA799_para.longitudinal_absolute_velocity));
    if(event_BA799_para.sys_lngstate ==1 && event_BA799_para.esp_veh_spd>=30 && fabs(event_BA799_para.lateral_distance)<=2.2
    &&(event_BA799_para.esp_veh_spd-event_BA799_para.longitudinal_absolute_velocity*3.6>=15) &&(event_BA799_para.longitudinal_distance/(event_BA799_para.esp_veh_spd/3.6-event_BA799_para.longitudinal_absolute_velocity)<1.5)
    &&(event_BA799_para.longitudinal_distance/(event_BA799_para.esp_veh_spd/3.6-event_BA799_para.longitudinal_absolute_velocity)>0)
    )
    {
        if (CompareValueIsZero(event_BA799_para.first_cond_trigger_time) == true)
        {
            event_BA799_para.first_cond_trigger_time = data.time;
        }
        else if(CompareValueIsZero(event_BA799_para.first_cond_trigger_time) == false
                 && (data.time - event_BA799_para.first_cond_trigger_time) > (100))
        {
            if (event_BA799_para.event_happen_flag != 1)
            {
                tmp_event_status = 1;
                event_BA799_para.event_happen_flag =1;
                retTigger.eventName = eventId;
                retTigger.triggerTime = Util::getMsTime();
                retTigger.recordTime = CommonOprTime::getTime();
                retTigger.beforeTime = 5000;
                retTigger.laterTime = 5000;
                return tmp_event_status;
            }
        }
    }
    else
    {
        event_BA799_para.first_cond_trigger_time = 0;
        event_BA799_para.event_happen_flag = 0;
    }
    //Log_Info("case 2 sys_lngstate:{},esp_veh_spd:{},longitudinal_distance:{},lateral_distance:{},width:{},second_cond_trigger_time:{},data_time:{}",event_BA799_para.sys_lngstate,event_BA799_para.esp_veh_spd
    //,event_BA799_para.longitudinal_distance,event_BA799_para.lateral_distance,event_BA799_para.width,event_BA799_para.second_cond_trigger_time,data.time);
    if(event_BA799_para.sys_lngstate ==0x01 && event_BA799_para.esp_veh_spd>=30 &&event_BA799_para.longitudinal_distance>=-3 &&(event_BA799_para.longitudinal_distance<=event_BA799_para.esp_veh_spd/3.6)
    &&(fabs(event_BA799_para.lateral_distance)-1.1-event_BA799_para.width/2<=0.4))
    {
        if (CompareValueIsZero(event_BA799_para.second_cond_trigger_time) == true)
        {
            event_BA799_para.second_cond_trigger_time = data.time;
        }
        else if(CompareValueIsZero(event_BA799_para.second_cond_trigger_time) == false
                 && (data.time - event_BA799_para.second_cond_trigger_time) > (100))
        {
            if (event_BA799_para.event_happen_flag != 1)
            {
                tmp_event_status = 1;

                retTigger.eventName = eventId;
                retTigger.triggerTime = Util::getMsTime();
                retTigger.recordTime = CommonOprTime::getTime();
                retTigger.beforeTime = 5000;
                retTigger.laterTime = 5000;
                return tmp_event_status;
            }
        }
        
    }
    else
    {
        event_BA799_para.second_cond_trigger_time = 0;
        event_BA799_para.event_happen_flag = 0;
    }
    //Log_Info("case 3 pd_status:{},sys_latstate:{},sys_lngstate:{},hu_nav_guigance_status:{},esp_long_accel:{},hu_road_curvature:{},esp_veh_spd:{},third_cond_trigger_time:{},data_time:{}",
    //event_BA799_para.pd_status,event_BA799_para.sys_latstate,event_BA799_para.sys_lngstate,event_BA799_para.hu_nav_guigance_status,event_BA799_para.esp_long_accel,
    //event_BA799_para.hu_road_curvature,event_BA799_para.esp_veh_spd,event_BA799_para.third_cond_trigger_time,data.time);
    if((event_BA799_para.pd_status==0x2 ||event_BA799_para.pd_status==0x3) && event_BA799_para.sys_latstate==0x1
    &&event_BA799_para.sys_lngstate ==0x01 &&event_BA799_para.hu_nav_guigance_status ==0x01 &&event_BA799_para.esp_long_accel>1.5
    &&((event_BA799_para.hu_road_curvature>0.02&&event_BA799_para.esp_veh_spd>50)||(event_BA799_para.hu_road_curvature>0.01&&event_BA799_para.esp_veh_spd>60)||(event_BA799_para.hu_road_curvature>0.007&&event_BA799_para.esp_veh_spd>70)))
    {

        if (CompareValueIsZero(event_BA799_para.third_cond_trigger_time) == true)
        {
            event_BA799_para.third_cond_trigger_time = data.time;
        }
        else if(CompareValueIsZero(event_BA799_para.third_cond_trigger_time) == false
                 && (data.time - event_BA799_para.third_cond_trigger_time) > (100))
        {
            if (event_BA799_para.event_happen_flag != 1)
            {
                tmp_event_status = 1;

                retTigger.eventName = eventId;
                retTigger.triggerTime = Util::getMsTime();
                retTigger.recordTime = CommonOprTime::getTime();
                retTigger.beforeTime = 5000;
                retTigger.laterTime = 5000;
                return tmp_event_status;
            }
        }
        
    }
    else
    {
        event_BA799_para.third_cond_trigger_time = 0;
        event_BA799_para.event_happen_flag = 0;
    }
    return tmp_event_status;
    
}
